package cn.edu.sctu.sctu_lysd.presenter;

import android.app.Activity;
import android.widget.EditText;

import cn.edu.sctu.sctu_lysd.bean.JsonBaseBeen;
import cn.edu.sctu.sctu_lysd.bean.RouteData;
import cn.edu.sctu.sctu_lysd.bean.TouristMessageBean;
import cn.edu.sctu.sctu_lysd.biz.GoodsMessageBiz;
import cn.edu.sctu.sctu_lysd.biz.TouristMessageBiz;
import cn.edu.sctu.sctu_lysd.cofig.CacheKeyConfig;
import cn.edu.sctu.sctu_lysd.net.NetService;
import cn.edu.sctu.sctu_lysd.net.RetrofitHelper;
import cn.edu.sctu.sctu_lysd.utils.L;
import cn.edu.sctu.sctu_lysd.utils.SharePreferenceUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wangfuxin on 2017-04-09.
 */

public class TouristMessagePresenter implements TouristMessageBiz.Presenter {

    private Activity mActivity;
    private TouristMessageBiz.TouristMessageView mView;
    private Call<JsonBaseBeen<RouteData>> routeCall = null;
    private NetService service = null;
    private SharePreferenceUtils mSharePreference;

    public TouristMessagePresenter(Activity activity, TouristMessageBiz.TouristMessageView mView) {
        this.mActivity = activity;
        this.mView = mView;
    }

    @Override
    public void start() {
        RetrofitHelper retrofitHelper = RetrofitHelper.getInstance();
        service = (NetService) retrofitHelper.getService(NetService.class);

        mSharePreference = SharePreferenceUtils.getInstance(mActivity);
    }

    @Override
    public String getInputSting(EditText view) {

        if (view != null) {
            return view.getText().toString();
        }

        return "";
    }


    @Override
    public void uploadMessage(TouristMessageBean bean) {

        L.e("getDestinationPre",bean.getDestinationPre());

       routeCall =  service.routes(mSharePreference.getString(CacheKeyConfig.CACHE_ACCESS_TOKEN),
                bean.getDestinationPre(),
                bean.getDestinationDetail(),
                bean.getOriginPre(),
                bean.getOriginDetail(),
                bean.getStartTime(),
                bean.getEndTime());

        routeCall.enqueue(new Callback<JsonBaseBeen<RouteData>>() {
            @Override
            public void onResponse(Call<JsonBaseBeen<RouteData>> call, Response<JsonBaseBeen<RouteData>> response) {
                L.e("responsewangfuxin", "response" + response.toString());

                try {

                    if (response.body().getMeta().getCode() == 200) {

                        mView.uploadSuccess();

                    }
                }catch (Exception e){

                    mView.uploadFailed();

                }



            }

            @Override
            public void onFailure(Call<JsonBaseBeen<RouteData>> call, Throwable t) {
                mView.uploadFailed();
            }
        });


    }

    @Override
    public void cancel() {
        if (routeCall != null) {
            routeCall.cancel();
        }
    }

}
